<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class goods_sku_mod extends module
{
    function __construct()
    {
        parent::__construct();
    }
    public function get_one($id)
    {
        return $this->db->get_one("select * from " . table('goods_sku') . " where id = " . $id);
    }
    public function get_list($id)
    {
        $row = $this->db->get_one("select * from " . table('goods_sku_index') . " where gid = " . $id . " limit 1");
        if ($row['id']) {
            if ($row['value'] == '0') {
                return $row;
            } else {
                $img_query = $this->db->get_all("select * from " . table('goods_sku_img') . " where gid = " . $id);
                if ($img_query) {
                    foreach ($img_query as $v) {
                        $img_data[$v['sku_id']] = $v;
                    }
                }
                $pro_price_arr = $this->module('goods_act')->seckill_price(array($id));
                $grp_price_arr = $this->module('goods_act')->group_price(array($id));
                $sku_result = $this->db->get_all("select * from " . table('goods_sku_index') . " where gid = " . $id);
                foreach ($sku_result as $val) {
                    $val_id[] = $val['value'];
                    $sku_val['sku_id'] = $val['id'];
                    $sku_val['price'] = $val['price'];
                    $sku_val['stock'] = $val['stock'];
                    if (isset($pro_price_arr[$val['id']])) {
                        $sku_val['act_price'] = $pro_price_arr[$val['id']];
                    }
                    if (isset($grp_price_arr[$val['id']])) {
                        $sku_val['grp_price'] = $grp_price_arr[$val['id']];
                    }
                    $json_data[$val['value']] = $sku_val;
                }
                $sku_json = json_encode($json_data);
                $val_id = implode(',', $val_id);
                $arr = explode(',', $val_id);
                $arr = array_unique($arr);
                $fquery = $this->db->get_all("select * from " . table('goods_sku') . " where id in(" . $row['value'] . ")");
                if ($fquery) {
                    foreach ($fquery as $k => $v) {
                        $pid_arr[] = $v['pid'];
                    }
                    $pid_in = implode(',', $pid_arr);
                    $query = $this->db->query("select * from " . table('goods_sku') . " where id in(" . $pid_in . ")");
                    while ($row = $this->db->fetch_array($query)) {
                        $pquery = $this->db->query("select * from " . table('goods_sku') . " where pid = " . $row['id']);
                        while ($prow = $this->db->fetch_array($pquery)) {
                            if (in_array($prow['id'], $arr)) {
                                $prow['img_name'] = $img_data[$prow['id']]['name'];
                                $prow['img_url'] = $img_data[$prow['id']]['images'];
                                $row['detail'][] = $prow;
                            }
                        }
                        $data[] = $row;
                    }
                    $datas['list'] = $data;
                    $datas['json'] = $sku_json;
                    return $datas;
                } else {
                    return false;
                }
            }
        } else {
            return false;
        }
    }
    function get_sku_goods($post_sku, $address_id = null)
    {
        foreach ($post_sku as $v) {
            $sku_arr[] = $v['sku_id'];
            $post_buy_num[$v['sku_id']] = $v['num'];
            if (isset($v['cart_id'])) {
                $post_cart_id[$v['sku_id']] = $v['cart_id'];
            }
        }
        $sku_id = implode(',', $sku_arr);
        $sku_all = $this->db->get_all("select * from " . table('goods_sku_index') . " where id in (" . $sku_id . ")");
        if ($sku_all) {
            foreach ($sku_all as $v) {
                $gidArr[] = $v['gid'];
            }
            $sku_img = $this->get_sku_img($gidArr);
            $gid_in = implode(',', $gidArr);
            $gquery = $this->db->get_all("select id,shop_id,title,price,images,market_price,stock,express from " . table('goods') . " where id in (" . $gid_in . ")");
            if ($gquery) {
                foreach ($gquery as $v) {
                    $goods_id[$v['id']] = $v;
                    $shop_arr[] = $v['shop_id'];
                    $express_tpl_arr[] = $v['express'];
                }
            }
            $pro_price_arr = $this->module('goods_act')->seckill_price($gidArr);
            foreach ($sku_all as $v) {
                $shop_id = $goods_id[$v['gid']]['shop_id'];
                $v['shop_id'] = $shop_id;
                $v['title'] = $goods_id[$v['gid']]['title'];
                $v['market_price'] = $goods_id[$v['gid']]['market_price'];
                $v['express'] = $goods_id[$v['gid']]['express'];
                $v['gid'] = $goods_id[$v['gid']]['id'];
                if (!empty($v['value'])) {
                    $val_arr = explode(',', $v['value']);
                    foreach ($val_arr as $vv) {
                        if (!empty($sku_img[$v['gid']][$vv]['images'])) {
                            $v['images'] = $sku_img[$v['gid']][$vv]['images'];
                            break;
                        }
                    }
                }
                if (empty($v['images'])) {
                    $v['images'] = $goods_id[$v['gid']]['images'];
                }
                $v['sku_id'] = $v['id'];
                $v['sku_name'] = unserialize($v['name']);
                if (isset($post_cart_id[$v['id']])) {
                    $v['cartid'] = $post_cart_id[$v['id']];
                }
                if (isset($pro_price_arr[$v['id']])) {
                    $v['price'] = $pro_price_arr[$v['id']];
                } else {
                    $v['price'] = $v['price'];
                }
                $v['stock'] = $v['stock'];
                $goods[$shop_id][] = $v;
            }
            $shop_data = $this->module('shop')->get_shop($shop_arr);
            $total_price = '0';
            foreach ($shop_data as $v) {
                $shop_total_price = '0';
                foreach ($goods[$v['id']] as $kk => $vv) {
                    $buy_num = $post_buy_num[$vv['sku_id']]['num'];
                    if ($vv['stock'] - $buy_num >= 0) {
                        $vv['stock_status'] = '1';
                        $goods_total_price = $vv['price'] * $buy_num;
                        $shop_total_price += $goods_total_price;
                    }
                    $vv['goods_status'] = '1';
                    $vv['buy_num'] = $buy_num;
                    $vv['goods_total_price'] = $goods_total_price;
                    $goods[$v['id']][$kk] = $vv;
                    $express_tpl_arr[] = $vv['express'];
                }
                $shop_pay[$v['id']]['sp_goods_price'] = $shop_total_price;
                $reduce_row = $this->module('goods_act')->reduce($v['id'], $shop_total_price);
                if ($reduce_row) {
                    $shop_pay[$v['id']]['sp_reduce_name'] = $reduce_row['name'];
                    $shop_pay[$v['id']]['sp_reduce_price'] = $reduce_row['reduce_price'];
                    $shop_total_price = $shop_total_price - $reduce_row['reduce_price'];
                }
                $express_price = isset($express_arr[$v['id']]) ? $express_arr[$v['id']] : '0';
                $shop_pay[$v['id']]['sp_express_price'] = $express_price;
                $shop_total_price = $shop_total_price + $express_price;
                $shop_pay[$v['id']]['sp_total_price'] = $shop_total_price;
                $total_price += $shop_total_price;
            }
            foreach ($shop_data as $v) {
                $v['goods_price'] = $shop_pay[$v['id']]['sp_goods_price'];
                $v['express_price'] = $shop_pay[$v['id']]['sp_express_price'];
                $v['total_price'] = $shop_pay[$v['id']]['sp_total_price'];
                if (isset($shop_pay[$v['id']]['sp_reduce_name'])) {
                    $v['reduce_name'] = $shop_pay[$v['id']]['sp_reduce_name'];
                    $v['reduce_price'] = $shop_pay[$v['id']]['sp_reduce_price'];
                }
                $v['goods'] = $goods[$v['id']];
                $goods_list[] = $v;
            }
            $result_data['list'] = $goods_list;
            $result_data['total_price'] = $total_price;
            return $result_data;
        } else {
            return false;
        }
    }
    function get_sku_img($gid_arr)
    {
        $gid_in = implode(',', $gid_arr);
        $query = $this->db->get_all("select * from " . table('goods_sku_img') . " where gid in (" . $gid_in . ")");
        if ($query) {
            foreach ($query as $v) {
                $sku_img[$v['gid']][$v['sku_id']] = array('name' => $v['name'], 'images' => $v['images']);
            }
            return $sku_img;
        } else {
            return false;
        }
    }
    public function sku_checkbox($cid)
    {
        if ($cid) {
            $wcid = 'cid in(0, ' . $cid . ')';
        } else {
            $wcid = 'cid = 0';
        }
        $query = $this->db->get_all("select * from " . table('goods_sku') . " where " . $wcid . " and pid = 0");
        if ($query) {
            foreach ($query as $v) {
                $id_arr[] = $v['id'];
            }
            $id_in = implode(',', $id_arr);
            $pquery = $this->db->get_all("select * from " . table('goods_sku') . " where pid in(" . $id_in . ")");
            if ($pquery) {
                foreach ($pquery as $v) {
                    $pid_arr[$v['pid']][] = $v;
                }
            }
            foreach ($query as $v) {
                if (isset($pid_arr[$v['id']])) {
                    $v['p_sku'] = $pid_arr[$v['id']];
                }
                $list[] = $v;
            }
            return $list;
        } else {
            return false;
        }
    }
    public function get_color_images($val, $update_arr = '')
    {
        $img_host = img_host();
        $img_size = img_size('30x30');
        if (!empty($update_arr)) {
            foreach ($update_arr as $v) {
                $aaa[$v['sku_id']] = $v;
            }
        }
        $val = rtrim($val, ',');
        if (!empty($val)) {
            $table = '<div class="name"><span>颜色备注附加图片</span></div>';
            $table .= '<table width="100%" class="sku_table">
							<tbody>';
            $query = $this->db->query("select * from " . table('goods_sku') . " where id in(" . $val . ")");
            while ($row = $this->db->fetch_array($query)) {
                $cfg_sku_color_img = cfg('sku_color_img');
                $cfg_color_img = !empty($cfg_sku_color_img) ? $cfg_sku_color_img : '1';
                if ($row['pid'] == $cfg_color_img) {
                    $count_num[] = '';
                    $table .= '<tr>
								 <td>' . $row['name'] . '</td>';
                    $table .= '<td>
									<div class="sku_attr_img_box"> 
										<input type="text" name="sku_img_name[]" value="' . @$aaa[$row['id']]['name'] . '" class="sku_remark" data-id="' . $row['id'] . '" placeholder="备注"/>
										<input type="hidden" name="sku_img_id[]" value="' . $row['id'] . '"/> 
										<input type="hidden" name="sku_img_value[]" value="' . @$aaa[$row['id']]['images'] . '" class="sku_img_value_' . $row['id'] . '"/>  
									</div>
									<div class="upload_albume">
										<a href="javascript:;" onclick="upload_manage(\'sku_img\',\'' . $row['id'] . '\')">上传图片</a>
									</div>
									<div class="sku_attr_img_show sku_show_' . $row['id'] . '"> ';
                    if (!empty($aaa[$row['id']]['images'])) {
                        $table .= '<img src="' . $img_host . $aaa[$row['id']]['images'] . $img_size . '">';
                    }
                    $table .= '		</div>
								</td>
							</tr>';
                }
            }
            $table .= '	</tbody>
					</table>';
            if (count(@$count_num) == '0') {
                return false;
            } else {
                return $table;
            }
        } else {
            return false;
        }
    }
    public function get_list_func($sku_id, $update_arr = null)
    {
        if ($sku_id) {
            $query = $this->db->get_all("select * from " . table('goods_sku') . " where id in (" . $sku_id . ")");
            if ($query) {
                foreach ($query as $v) {
                    $datas[$v['pid']][] = $v['id'];
                    $sku_val_cn[$v['id']] = $v['name'];
                }
            }
            ksort($datas);
            foreach ($datas as $v) {
                $data[] = $v;
                $cen_data[] = count($v);
            }
            $result = $this->combineDika($data);
            $tables = '	<div class="name"><span>宝贝销售规格</span></div>';
            $tables .= '<table width="100%">
							<thead>
								<tr>';
            foreach ($datas as $key => $val) {
                $sku_key[] = $key;
            }
            $sku_key_in = implode(',', $sku_key);
            $key_query = $this->db->get_all("select * from " . table('goods_sku') . " where id in (" . $sku_key_in . ")");
            if ($key_query) {
                foreach ($key_query as $k => $v) {
                    $tables .= '	<th width="100">' . $v['name'] . '</th>';
                    $kname[$k] = $v['name'];
                }
            }
            $tables .= '			<th width="60">价格</th>
									<th width="60">库存</th>
									<th width="100">条形码</th>
									<th></th>
								</tr>
							</thead>
						<tbody>';
            $count_num = count($datas);
            $result_count_num = count($result);
            foreach ($result as $key => $val) {
                $tables .= '<tr>';
                $ii = '1';
                for ($i = 0; $i < $count_num; $i++) {
                    $cen_data_result = $cen_data[$i];
                    $ii = $cen_data[$i] * $ii;
                    $rowspan = $result_count_num / $ii;
                    if ($key % $rowspan == '0') {
                        $tables .= '<td rowspan="' . $rowspan . '" class="sku_img_' . $val[$i] . '">' . $sku_val_cn[$val[$i]] . '</td>';
                    }
                    $val_arr[] = $val[$i];
                    $sku_name_row[] = array($kname[$i] => $sku_val_cn[$val[$i]]);
                }
                $val_name = json_encode($sku_name_row);
                unset($sku_name_row);
                $val_in = implode(',', $val_arr);
                unset($val_arr);
                $tables .= '';
                if ($update_arr) {
                    $tables .= '
						<td><input type="text" name="sku_price[]" class="fill_price" value="' . @$update_arr[$val_in]['price'] . '" size="8"/></td>
						<td><input type="text" name="sku_stock[]" class="fill_stock" value="' . @$update_arr[$val_in]['stock'] . '" size="8"/></td> 
						<td><input type="text" name="sku_code[]"  class="sku_code" value="' . @$update_arr[$val_in]['code'] . '" size="12"/></td>
						<td>
							<input type="hidden" name="sku_id[]"  value="' . @$update_arr[$val_in]['id'] . '"/>
							<input type="hidden" name="sku_val[]" value="' . $val_in . '"/> 
						</td>';
                } else {
                    $tables .= '
						<td><input type="text" name="sku_price[]" class="fill_price" size="8"/></td> 
						<td><input type="text" name="sku_stock[]" class="fill_stock" size="8"/></td>
						<td><input type="text" name="sku_code[]" class="sku_code" size="12"/></td>
						<td><input type="hidden" name="sku_val[]" value="' . $val_in . '"/></td>  ';
                }
                $tables .= '</tr>';
            }
            $tables .= '</tbody>
					</table> ';
            return $tables;
        }
    }
    private function combineArray($arr1, $arr2)
    {
        $result = array();
        foreach ($arr1 as $item1) {
            foreach ($arr2 as $item2) {
                $temp = $item1;
                $temp[] = $item2;
                $result[] = $temp;
            }
        }
        return $result;
    }
    private function combineDika($data)
    {
        $cnt = count($data);
        $result = array();
        foreach ($data[0] as $item) {
            $result[] = array($item);
        }
        for ($i = 1; $i < $cnt; $i++) {
            $result = $this->combineArray($result, $data[$i]);
        }
        return $result;
    }
}